<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['pcd']=$_SESSION['pcd']?:array();
$member=$this->model('member')->checklogin();
if($_RQ['sale_id']){
	$this->model('member')->upsale_id($_RQ['sale_id'],$member);
}
$_SESSION['area_code']=$_RQ['area_code']?:$_SESSION['area_code'];
$return['area_code']=$_SESSION['area_code'];
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['indexpai']=array();	
	$return['shopset'] = plugin('jy_weishop')->plugin_setting('shopset');
	$return['setting']['pluginnav']=$return['pluginset']['bankuai'];
	$return['setting']['hdps']=$return['pluginset']['hdps'];
	$p_group=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','isshow'=>'1','parentid'=>'0'),'','','paixu desc');
	//子分类
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$params['saletype likes']=array('1');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','image','price','total','active_plugin','active_id');
	if($p_group){
		$i=0;
		foreach($p_group as &$item){
			$item['c_group']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','isrecommand'=>'1','parentid'=>$item['id']),'','paixu asc');
			$item['goodslist']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('1'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			$item['goodsnew']=pdo_getslice('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'saletype likes'=>array('2'),'pcate'=>$item['id']),array($pindex,$psize),$total,$fields,'','paixu desc');
			$item['style']=style($item['id']);
			$i=$i+1;
			$item['i']=$i;
			if($item['c_group']){
				foreach($item['c_group'] as &$row){
					$params['ccate']=$row['id'];
					$row['goods']=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc');
				}
			}
		}
	}
	//公告列表
	$notice_list = pdo_fetchall('SELECT * FROM ' . tablename('jy_weishop_notice') . ' WHERE uniacid=:uniacid AND status=1  ORDER BY id DESC limit 4', array(':uniacid' => $_SESSION['uniacid']));
	if($notice_list){
		foreach($notice_list as &$notice){
			$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
		}
	}
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	$return['orderlist']=pdo_getslice('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),array($pindex,$psize),$total,'','','createtime desc');
	foreach($return['orderlist'] as &$ov){
		$ordergoods=pdo_get('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$ordergoods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
		$ov['goods']=array_merge($ordergoods,$goodsinfo);
		$ov['createtime']=date('Y/m/d H:i:s',$ov['createtime']);
		$ov['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']),'total');
	}
	$return['allprice']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'deleted'=>0,'status'=>0),'price');
	//获取用户信息
	$return['extradata']=array();
	$return['user']=model('core/member')->basic($_SESSION['uid']);
	$return['login']=$_SESSION['uid']?1:2;
	$return['notice_list']=$notice_list;
	$return['p_group']=$p_group;
	$return['params']['pid']=0;
	set_html('title',$return['pluginset']['title']?$return['pluginset']['title']:'新零售');
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title']?$return['pluginset']['share_title']:'新零售',
		'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'新零售，主流经典分销新零售，新购买模式！',
		'link'=>custom_url('jy_area_fee/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>$return['pluginset']['share_image']?tomedia($return['pluginset']['share_image']):tomedia($return['pluginset']['icon'])
	);
	if(empty($return['orderlist'])){
		$return['coupon_wait']=model('jy_weishop/task')->pop_coupon_wait();
		if(!$return['coupon_wait']){
			$return['coupon_hd']=model('jy_weishop/task')->pop_coupon_hd();
			if(!$return['coupon_hd']){
				$return['game_wait']=model('jy_weishop/task')->pop_game();
			}
		}
	}
	$return['pluginset']['footernav']=$this->model('member')->up_footernav();
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}else{
		//有关键词时，分类ID不检索！
		if(($_RQ['pcate'] || $_RQ['pid']) && $_RQ['pid']!=-1){
			$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
			$params['pcate']=$pcate;
			$return['params']['pcate']=$pcate;
		}
		if($_RQ['ccate'] || $_RQ['cid']){
			$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
			$params['ccate']=$ccate;
			$return['params']['ccate']=$ccate;
		}
	}
	if(!$member['agent_id']){
		if($params['ccate'] || $params['pcate'] || $_RQ['keyword']){
			$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
		}
	}
	$member=$this->get('member',array('mid'=>$_SESSION['uid']));
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
	);
	if($goodsids){
		$goodsids=array_keys($goodsids);
		$params['goods_id']=$goodsids;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize =10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$orderby='id desc';
	$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
	);
	//只有经销商可以看到商品
	if($member['is_agent'] && !$member['agent_id'] && !$member['type'] && !$member['dian_mid']){
		$parameter['params']['ignore']=0;
		$list=$this->get_list('goods',$parameter,$total);
	}elseif($member['agent_id']){
		if($member['dian_mid']){
			$dian_mid=$member['dian_mid'];
		}elseif($member['daili_id']){
			$dian_mid=$member['daili_id'];
		}else{
			$agent=$this->get('member',array('mid'=>$member['agent_id']));
			$dian_mid=$agent['dian_mid']?:$member['agent_id'];
		}
		$merchid=model('jy_merch/model')->checkadd_merch($dian_mid);
		$parameter['params']['merchid']=$merchid;
		$list=pdo_getlist('jy_weishop_goods',$parameter,$total);
	}
	if(!$list){
		exi('none');
	}else{
		foreach($list as &$itemv){
			$itemv['goods_id']=$itemv['goods_id']?:$itemv['id'];
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$itemv['goods_id']),array('title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit'));
			$itemv=array_merge($goods,$itemv);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['goods_id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			if($this->plugin_exists('jy_merch')){
				$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['goods_id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['goods_id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['goods_id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['goods_id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
				}
				//已选规格
				$selectspecs = array();
				$selespename = '';
				$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['goods_id']),'specs');				
				$selectspecs = str_array($specs,'_');
				if($selectspecs){
						foreach($selectspecs as $k=>&$spid){
							$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
							$selespename .=$title.'，';
						}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['goods_id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
			$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			$itemv['id']=$itemv['goods_id'];
			$itemv['active_plugin']='jy_area_fee';
			$itemv['url']='jy_weishop/goods.detail//id='.$itemv['id'];
			if($member['is_agent'] && !$member['type']){
				$itemv=model('jy_weishop/activity')->get_upgoods($itemv);
				$itemv['url']='jy_weishop/goods.detail//active_plugin=jy_area_fee&id='.$itemv['id'];
			}
		}
	}
	exi($list);
}
if($_RQ['op']=='getbufee'){
	$sale_id=$_RQ['sale_id'];
	$member=$this->get('member',array('mid'=>$sale_id));
	if($member['dian_mid']){
		$isget=$this->get('log',array('mid'=>$_SESSION['uid'],'dian_mid'=>$member['dian_mid'],'status'=>1));
		if($isget['id']){
			exi('您已经领取了补偿金，不能重复领取！','error','/index/main/');
		}
		$dian=$this->get('member',array('mid'=>$member['dian_mid']));
		$dian['downdata']=json_decode($dian['downdata'],true);
		if($dian['downdata']['bufee']){
			$params=array(
							'mid'=>$_SESSION['uid'],
							'dian_mid'=>$member['dian_mid'],
							'msg'=>'业务员发补偿金',
							'fee'=>$dian['downdata']['bufee'],
							'status'=>1,
			);
			$dian_credit=pdo_getcolumn('core_members',array('id'=>intval($member['dian_mid'])),'credit'.$this->setting['bufee_type']);
			if($dian_credit>=$dian['downdata']['bufee']){
					model('core/member')->member_credit($_SESSION['uid'],'credit'.$this->setting['bufee_type'],$dian['downdata']['bufee'],true,'jy_area_fee','业务员发补偿金');
					model('core/member')->member_credit($member['dian_mid'],'credit'.$this->setting['bufee_type'],$dian['downdata']['bufee'],false,'jy_area_fee','业务员发补偿金');					
					$this->set_log($params,0);
					exi('领取补偿金成功！','','/index/main/');
			}else{
					if($dian['downdata']['bufee']>0){
						$params['status']=0;
						$this->set_log($params,0);
					}
					exi('上级经销商《补偿金》资金池不足！','error','/index/main/');
			}
		}
	}else{
		exi('请找业务员获取补偿金！','error','/index/main/');
	}
	exi($return);
}
function style($id)
{
	switch ($id%7) {	
		case 0: return "style-brown";
		case 1: return 'style-red';
		case 2: return "style-pink";
		case 3: return "style-orange";
		case 4: return "style-green";
		case 5: return "style-blue";
		case 6: return "style-purple";
		case 7: return "style-brown";
		default: }
}
	
?>